package binarytree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/20 11:51
 * https://leetcode-cn.com/problems/binary-tree-paths/
 * 给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
 *
 */
public class BinaryTreePaths {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * dfs 前序遍历
     * @param root
     * @return
     */
    public List<String> binaryTreePaths1(TreeNode root) {
        //存放经过的节点
        Stack<TreeNode> stack = new Stack<>();
        //存放每个节点当前的路径
        Stack<String> paths = new Stack<>();
        //存放最终的结果
        List<String> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        stack.push(root);
        paths.push(String.valueOf(root.val));
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            String path = paths.pop();
            if (pop.left == null && pop.right == null) {
                result.add(path);
            }
            if (pop.right != null) {
                stack.add(pop.right);
                paths.push(path + "->" + pop.right.val);
            }
            if (pop.left != null) {
                stack.add(pop.left);
                paths.push(path + "->" + pop.left.val);
            }
        }
        return result;
    }

    /**
     * 递归写法
     * @param root
     * @return
     */
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        List<Integer> paths = new ArrayList<>();
        traversal(root, paths, result);
        return result;
    }

    private void traversal(TreeNode root, List<Integer> paths, List<String> result) {
        paths.add(root.val);
        if (root.left == null && root.right == null) {
            //此时路径到达根节点
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < paths.size() - 1; i++) {
                s.append(paths.get(i)).append("->");
            }
            s.append(paths.get(paths.size() - 1));
            result.add(s.toString());
            return;
        }
        if (root.left != null) {
            //判断此节点是否到达根节点
            traversal(root.left, paths, result);
            //回溯一个节点
            paths.remove(paths.size() - 1);
        }
        if (root.right != null) {
            traversal(root.right, paths, result);
            paths.remove(paths.size() - 1);
        }
    }

    /**
     * 递归写法（精简版，看起来精简，但运行速度慢） 用String代替List
     * @param root
     * @return
     */
    public List<String> binaryTreePaths2(TreeNode root) {
        List<String> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        String paths = "";
        traversal1(root, paths, result);
        return result;
    }

    private void traversal1(TreeNode root, String paths, List<String> result) {
        paths += root.val;
        if (root.left == null && root.right == null) {
            result.add(paths);
            return;
        }
        //此时不相等
        if (root.left != null) {
            traversal1(root.left, paths + "->", result);
        }
        if (root.right != null) {
            traversal1(root.right, paths + "->", result);
        }
    }


    public static void main(String[] args) {

    }
}
